// pages/teamDetail/index.js
const util = require('../../utils/util')
Page({


  /**
   * 页面的初始数据
   */
  data: {
    teamData: {},
    ApplicationList: [],
    currentApplication:{},
    apply_check: 0,
    is_myTeam: false,
    is_member: false,
    showMember: false,
    showModal: false,
    memberlist: [],
    showApply: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const id = options.id;
    this.getteamdetail(id)
  },
  getteamdetail(id) {
    wx.cloud.database().collection('teamlist').where({
      _id: id
    }).get({
      success: (res) => {
        let teamData = res.data[0]
        let member=teamData.member
        teamData.date = util.formatTime(new Date(teamData.date))
        let useropenid = wx.getStorageSync('OPENID')
        this.setData({
          teamData: teamData,
          useropenid: useropenid,
        })
        if (teamData._openid == useropenid) {
          this.getApplications(id)
          this.setData({
            is_myTeam: true,
            showApply: true
          })
        }
        member.forEach(item => {
          if (item.openid == useropenid) {
            this.setData({
              is_member: true
            })
          }
        })

      },
      fail: (err) => {
        console.err(err)
      }
    })

  },
  //获取申请记录
  getApplications: function (id) {
    wx.cloud.database().collection('applications').where({
      teamid: id
    }).get({
      success:res=>{
        console.log(res)
        let ApplicationList = res.data
        this.setData({
          ApplicationList:ApplicationList
        })
      }
    })
  },
  linkteam: function (e) {
    let tele = e.currentTarget.dataset.phone;
    wx.showModal({
      title: '联系电话为：',
      content: tele,
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '复制',
      confirmColor: '#3CC51F',
      success: (result) => {
        if (result.confirm) {
          wx.setClipboardData({
            data: tele,
          })

        }
      },
      fail: () => {
        return false
      },
      complete: () => {},
    })

  },
  showapplication: function (e) {
    let appindex = e.currentTarget.dataset.appindex
    let ApplicationList = this.data.ApplicationList
    this.setData({
      showModal: true,
      currentApplication: ApplicationList[appindex]
    })
  },
  showMembers: function () {
    let members = this.data.teamData.member
    let showMember = !this.data.showMember
    this.setData({
      memberlist: members,
      showMember: showMember
    })
  },
  modal_click_Hidden: function () {
    this.setData({
      showMember: false,
      showModal: false
    })
  },
  addteam: function (e) {
    let teamid = e.currentTarget.dataset.teamid,
      teamtype = e.currentTarget.dataset.type
    let openid = e.currentTarget.dataset.openid
    let member = e.currentTarget.dataset.member
    let meopenid = wx.getStorageSync('OPENID')
    if (meopenid === openid) {
      wx.showToast({
        title: '不能加入自己发布的组队！',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    let ismember = false
    member.forEach(item => {
      if (item.openid == meopenid) {
        ismember = true
      }
    })
    if (ismember) {
      wx.showToast({
        title: '不能重复加入一个队伍！',
        icon: 'none',
        duration: 2000,
      })
      return
    }

    wx.showModal({
      title: '提示',
      content: '确定申请吗？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: (result) => {
        if (result.confirm) {
          wx.navigateTo({
            url: `/pages/application/application?teamid=${teamid}&teamtype=${teamtype}`,
          })

        }
      },
      fail: () => {
        return false
      },
      complete: () => {},
    })
  },
  //拒绝申请
  reject: function (e) {
    let applyid = e.currentTarget.dataset.applyid
    this.setData({
      showModal: false,
    })
    db.collection('applications').doc(applyid).update({
      data: {
        approval: -1
      },
      success: res => {
        wx.showToast({
          title: '审批成功',
          icon: 'none',
          image: '',
          duration: 1500,
          mask: false,
          success: (result) => {},
          fail: () => {},
          complete: () => {
            that.setData({
              showModal: false
            })
          },
        })
      }
    })
  },
  //同意申请
  agree: function (e) {
    let that = this
    let currentApplication = this.data.currentApplication
    let member = this.data.teamData.member
    let vacancy = this.data.teamData.vacancy
    const db = wx.cloud.database()
    let memberid = currentApplication._openid
    let ismember = false
    member.forEach(item => {
      if (item.openid == memberid) {
        item.wxid = currentApplication.wxid
        item.nickname = currentApplication.nickname
        ismember = true
      }
    })
    if (ismember) {
      wx.showToast({
        title: '该用户已加入',
        icon: 'none',
        duration: 2000,
        success: (res) => {
          that.setData({
            showModal: false
          })
        }
      })
    }
    member.push({
      "openid": currentApplication._openid,
      "nickname": currentApplication.nickname,
      "wxid": currentApplication.wxid
    })
    db.collection('teamlist').doc(this.data.teamData._id).update({
      data: {
        vacancy: vacancy - 1,
        member: member,
      },
      success: function (res) {
        try {
          db.collection('applications').doc(currentApplication._id).update({
            data: {
              approval: 1
            }
          })
          wx.showToast({
            title: '审批成功',
            icon: 'none',
            image: '',
            duration: 1500,
            mask: false,
            success: (result) => {},
            fail: () => {},
            complete: () => {
              that.setData({
                showModal: false
              })
            },
          })
          this.getApplications(this.data.teamData._id)
        } catch (error) {
          console.error(error)
        }
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})